<template>
	<div class="dict-card">
		<!--查询表单-->
		<el-card class="box-card mb10" shadow="never">
			<template #header>
				<span class="card-title">字典类型</span>
			</template>
			<el-form ref="searchFormRef" class="search-form" :inline="true" label-position="left" label-width="80px" :model="searchForm">
				<el-row :gutter="30" justify="start">
					<el-col :span="8">
						<el-form-item class="form-item" label="字典名称:" prop="dictName">
							<el-input v-model="searchForm.dictName" placeholder="请输入字典名称" />
						</el-form-item>
					</el-col>
					<el-col :span="8">
						<el-form-item class="form-item" label="字典类型:" prop="dictType">
							<el-input v-model="searchForm.dictType" placeholder="请输入字典类型" />
						</el-form-item>
					</el-col>
					<el-col :span="8">
						<div class="search-btn">
							<el-button type="primary" @click="handleSearch">查询</el-button>
							<el-button @click="handleReset">重置</el-button>
						</div>
					</el-col>
				</el-row>
			</el-form>
		</el-card>
		<el-card class="box-card" shadow="never" style="height: 721px">
			<static-table :tableData="tableData" :isCurrent="true" rowId="dictId" :loading="loading" @on-current-change="getCurrentChange">
				<template #vux-search>
					<el-row :gutter="12" class="mb10">
						<el-button class="ml8" type="primary" v-has="'system:dictType:add'" @click="handleAdd">
							<el-icon><ele-Plus /></el-icon>
							&nbsp;新增
						</el-button>
						<el-button class="margin-left-10" v-has="'system:dictType:clear'" type="warning" @click="handleClearCache()">清除缓存</el-button>
					</el-row>
				</template>
				<template #vux-column>
					<vxe-column field="selection" fixed="left" type="checkbox" width="60" />
					<vxe-column field="dictName" min-width="150" show-overflow="tooltip" title="字典名称" />
					<vxe-column field="dictType" min-width="120" show-overflow="tooltip" title="字典类型" />
					<vxe-column field="status" min-width="120" title="状态">
						<template #default="{ row }">
							<span style="margin-right: 10px">{{ row.status == '0' ? '正常' : '停用' }}</span>
							<el-switch active-color="#00B42A" inactive-color="#F04141" :model-value="row.status == '0' ? true : false" @change="handleSwitch(row)" />
						</template>
					</vxe-column>
					<vxe-column field="operation" fixed="right" min-width="120" title="操作">
						<template #default="{ row }">
							<div class="vxe-action">
								<a @click="handleUpdate(row)" v-has="'system:dictType:update'">修改</a>
								<a @click="handleDelete(row)" v-has="'system:dictType:delete'">删除</a>
							</div>
						</template>
					</vxe-column>
				</template>
			</static-table>
			<el-pagination background :current-page="pageNum" :layout="layout" :page-size="pageSize" :total="dataCount" @current-change="handleCurrentChange" @size-change="handleSizeChange" />
		</el-card>
		<EditDictTypeModal ref="editModalRef" @peak-modal-ok="handleModalOk" />
	</div>
</template>
<script setup>
import global from '@/utils/global'
import useTableList from '@/hooks/useTableList'
import useModal from '@/hooks/useModal'
const EditDictTypeModal = defineAsyncComponent(() => import('./EditDictTypeModal.vue'))
const emit = defineEmits(['emitDictType'])

/*
 *@Description: 状态初始化
 *@MethodAuthor: liuYuFeng
 *@Date: 2023-08-28 10:11:35
 */

const state = reactive({
	searchForm: {
		dictName: '',
		dictType: '',
	},
	selectionAry: [],
	searchFormRef: null,
})
const { searchForm, searchFormRef } = toRefs(state)

const queryUrl = '/meta-system/web/v1/sysDictType/list'
const { loading, dataCount, handleInitQuery, handleModalOk, tableData, pageNum, pageSize, layout, handleSizeChange, handleCurrentChange } = useTableList(queryUrl, state.searchForm)

const { editModalRef, handleAdd, handleUpdate } = useModal()

/*
 *@Description: 方法模块
 *@MethodAuthor: liuYuFeng
 *@Date: 2023-08-28 10:16:03
 */
const handleSearch = () => {
	handleInitQuery()
}
const handleReset = () => {
	state.searchFormRef.resetFields()
	handleInitQuery()
}
const getCurrentChange = row => {
	emit('emitDictType', row.dictType)
}
const handleDelete = row => {
	const ids = []
	if (row) {
		ids.push(row.dictId)
	} else {
		if (state.selectionAry.length == 0) {
			global.$baseMessage('请先选择要删除的数据！', 'warning')
			return false
		}
		state.selectionAry.forEach(item => {
			ids.push(item.dictId)
		})
	}
	global.$baseConfirm(
		row ? `是否确认删除名称为"--【${row.dictName}】--"的数据?` : '是否确认删除当前选中的数据？',
		'提示',
		() => {
			global.$fetchPostData(`/meta-system/web/v1/sysDictType/remove`, { removeIdList: ids }).then(data => {
				if (data.code == 200) {
					global.$baseMessage(data.msg, 'success')
					handleModalOk(2)
				} else {
					global.$baseMessage(data.msg, 'error')
				}
			})
		},
		() => {}
	)
}
//switch 切换
const handleSwitch = row => {
	global.$baseConfirm(`是否确认更改名称为"--【${row.dictName}】--"的数据?`, '提示', () => {
		const user = row
		user.status = row.status == 0 ? 1 : 0
		global.$fetchPostData(`/meta-system/web/v1/sysDictType/editDictType`, user).then(data => {
			if (data.code == 200) {
				global.$baseMessage(data.msg, 'success')
				handleModalOk(2)
			} else {
				global.$baseMessage(data.msg, 'error')
			}
		})
	})
}
// 清空缓存
const handleClearCache = () => {
	global.$fetchPostData(`/meta-system/web/v1/sysDictType/clearCache`).then(data => {
		if (data.code == 200) {
			global.$baseMessage(data.msg, 'success')
		} else {
			global.$baseMessage(data.msg, 'error')
		}
	})
}
</script>
<style lang="scss" scoped>
.card-title {
	font-size: 16px;
}
.box-card {
	:deep() {
		.el-card__header {
			padding-left: 14px !important;
		}
	}
}
</style>
